//
//  ICGroupSearchController.m
//  ICome
//
//  Created by ENN on 16/5/6.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICGroupSearchController.h"
#import "ICMessageTableViewCell.h"
#import "ICMessageDatabase.h"
#import "ICGroup.h"
#import "ICNavigationController.h"
#import "ICTitleView.h"
#import "UIImage+Extension.h"

@interface ICGroupSearchController ()<ICTitleViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)ICTitleView *titleView;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)ICEmptyStyleView *emptyView;


@end

@implementation ICGroupSearchController

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    self.navigationController.navigationBarHidden = NO;
    [self.titleView.searchBar resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUNDCOLOR;
    // 搜索头部控件定制化调整
    self.titleView = [[ICTitleView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, [ICTools getNavbarHeight]+[ICTools getStatusbarHeight])];
    self.titleView.delegate     = self;
    self.titleView.backgroundColor  = NE_BACKGROUND_COLOR;
    self.titleView.searchBar.backgroundColor = [ICTools getRgbColor:0x5CACFE];
    self.titleView.searchBar.tintColor = [UIColor whiteColor];
    self.titleView.searchBar.textColor = [UIColor whiteColor];
    [self.titleView.searchBar setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    UIImage *image = [UIImage  imageNamedFromiComeSDKBundle:@"icon_search"];
    self.titleView.searchBar.iconImage.image = [image imageWithColor:[UIColor whiteColor]];
    
    [self.view addSubview:self.titleView];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.definesPresentationContext = YES;
    //简单点说就是automaticallyAdjustsScrollViewInsets根据按所在界面的status bar，navigationbar，与tabbar的高度，自动调整scrollview的 inset,设置为no，不让viewController调整，我们自己修改布局即可~
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    [self setUI];

}

- (void)setUI {
    self.tableView.backgroundColor = BACKGROUNDCOLOR;
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    self.tableView.frame = CGRectMake(0, [ICTools getNavbarHeight]+[ICTools getStatusbarHeight], App_Frame_Width, APP_Frame_Height - [ICTools getNavbarHeight]-[ICTools getStatusbarHeight]);
    
    [self.tableView addSubview:self.emptyView];

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.titleView.searchBar resignFirstResponder];
}

#pragma mark - UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICMessageTableViewCell *cell = [ICMessageTableViewCell cellWithTableView:tableView];
    if (indexPath.row == self.dataArray.count - 1) {
        [cell setBottomLineStyle:CellLineStyleNone];
    }
    else {
        [cell setBottomLineStyle:CellLineStyleDefault];
    }
    cell.isSearch = YES;
    cell.keyString = self.keyString;
    cell.group = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICGroup *group = self.dataArray[indexPath.row];
    [ICMessageHelper updateGroupReadWithGroupId:group.gId];
    if ([self.delegate respondsToSelector:@selector(selectRow:)]) {
        [self.delegate selectRow:group];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)cancelBtnClicked {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)searchText:(NSString *)text {
    if (text.length > 0) {
        WEAKSELF
        [ICMessageDatabase searchGroupWithSearchText:text finish:^(NSArray *array, NSError *error) {
            weakSelf.keyString = text;
            weakSelf.dataArray = array;
        }];
    } else {
        self.dataArray = nil;
    }
}

#pragma mark - Private

- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray   = dataArray;
    self.emptyView.hidden = YES;
    if (dataArray.count > 0) {
        [self.tableView reloadData];
    } else {
        self.emptyView.hidden = NO;
//        [self.tableView addSubview:self.emptyStyleLable];
//        self.emptyStyleLable.text = @"暂无搜索结果";
        [self.tableView reloadData];
    }
}


#pragma mark - Getter

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, APP_Frame_Height) style:UITableViewStylePlain];
        //UITableViewStylePlain
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.userInteractionEnabled = YES;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

-(ICEmptyStyleView *)emptyView {
    if (!_emptyView) {
        _emptyView = [[ICEmptyStyleView alloc] initWithTitle:@"暂无搜索结果" placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_message_empty"] imageURL:@""];
        _emptyView.hidden = YES;
        _emptyView.frame = CGRectMake(0, 0, App_Frame_Width, APP_Frame_Height);
    }
    return _emptyView;
}


//- (UIStatusBarStyle)preferredStatusBarStyle
//{
//    return UIStatusBarStyleLightContent;
//}

@end
